﻿#region imports

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Codeplex.ODataTFS.Model.Entities;
using Microsoft.TeamFoundation.Server;

#endregion

namespace Codeplex.ODataTFS.Model.Serialization
{
	public class TFSProjectProxy : TFSBaseProxy
	{
		public TFSProjectProxy(Uri uri, ICredentials credentials)
			: base(uri, credentials)
		{
		}

		public IEnumerable<Project> GetProjectsByProjectCollection()
		{
			if (Cache[GetProjectsByProjectCollectionHashKey()] == null)
			{
				Cache[GetProjectsByProjectCollectionHashKey()] = RequestProjectsByProjectCollection();
			}

			return (IEnumerable<Project>) Cache[GetProjectsByProjectCollectionHashKey()];
		}

		private IEnumerable<Project> RequestProjectsByProjectCollection()
		{
			var css = TfsConnection.GetService<ICommonStructureService3>();
			var teamProjects = css.ListAllProjects();
			return teamProjects.Select(p => p.ToModel(TfsConnection.Name));
		}

		private string GetProjectsByProjectCollectionHashKey()
		{
			return "TFSProjectProxy.GetProjectsByProjectCollection";
		}
	}
}
